/*
 * @Descripttion:
 * @version:
 * @Author: congsir
 * @Date: 2025-11-17 23:43:06
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2025-11-22 15:53:43
 */
import { createSlice, createAsyncThunk } from '@reduxjs/toolkit'
import { getBanners } from '../service/recommend'

export const fetchBannerDataAction = createAsyncThunk(
  'banners',
  async (arg, { dispatch }) => {
    const res = await getBanners()
    dispatch(chanBannersAction(res.banners))
  }
)

interface IRecommendState {
  banners: any[]
}
const initialState: IRecommendState = {
  banners: []
}
const recommendSlice = createSlice({
  name: 'recommend',
  initialState,
  reducers: {
    //异步获取数据的二种方法
    chanBannersAction(state, { payload }) {
      state.banners = payload
    }
  }
  //异步获取数据的一种方法
  // extraReducers: (builder) => {
  //   builder
  //     .addCase(fetchBannerDataAction.pending, () => {
  //       console.log('pending')
  //     })
  //     .addCase(fetchBannerDataAction.fulfilled, (state, { payload }) => {
  //       state.banners = payload
  //     })
  //     .addCase(fetchBannerDataAction.rejected, () => {
  //       console.log('rejected')
  //     })
  // }
})
export const { chanBannersAction } = recommendSlice.actions
export default recommendSlice.reducer
